package com.example.yunbiji0314.controller;


import com.example.yunbiji0314.pojo.Category;
import com.example.yunbiji0314.service.CategoryService;
import com.example.yunbiji0314.util.Code;
import com.example.yunbiji0314.util.PageUtil;
import com.example.yunbiji0314.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/findByUserId")
    // 要想访问这个接口需要有CHECKITEM_AD权限 并且登录
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")
    @ApiOperation(value = "根据用户id查询分类")
    public Result findByUserId(@RequestBody PageUtil pageUtil){
        System.out.println(pageUtil);
        PageInfo<Category> byUserId = categoryService.findByUserId(pageUtil);
        if(pageUtil.getObject()!=null){
            if(byUserId!=null){
                return new Result(Code.GET_OK, byUserId, "查询成功");
            }else {
                return new Result(Code.GET_ERR, "查询失败");
            }
        }else {
            return new Result(Code.GET_ERR, "用户丢失了");
        }
    }

    @PostMapping("/add")
    // 要想访问这个接口需要有CHECKITEM_AD权限 并且登录
    @PreAuthorize("hasAnyAuthority('ORDERSETTING')")
    @ApiOperation(value = "添加分类")
    public Result add(@RequestBody Category category){
        int i = categoryService.add(category);
        if(i>0){
            return new Result(Code.ADD_OK, "添加成功");
        }else {
            return new Result(Code.ADD_ERR, "添加失败");
        }
    }

    @GetMapping("/delete01/{id}")
    @ApiOperation(value = "删除分类")
    public Result delete(@PathVariable Integer id){
        int i = categoryService.delete(id);
        if(i>0){
            return new Result(Code.DELETE_OK, "删除成功");
        }else {
            return new Result(Code.DELETE_ERR, "删除失败");
        }
    }

    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id查询分类")
    public Result findById(@PathVariable Integer id){
        System.out.println(id);
        Category category = categoryService.findById(id);
        if(category!=null){
            return new Result(Code.GET_OK, category, "查询成功");
        }else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    @GetMapping("/selectleixingId/{id}")
    @ApiOperation(value = "根据用户id查询分类")
    public Result selectleixingId(@PathVariable Integer id){
        System.out.println(id);
        List<Category> category = categoryService.selectleixingId(id);
        if(category!=null){
            return new Result(Code.GET_OK, category, "查询成功");
        }else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    @PostMapping("/selectAll")
    @ApiOperation(value = "查询所有分类")

    public Result selectAll(){
        List<Category> category = categoryService.selectAll();
        if(category!=null&&category.size()>0){
            return new Result(Code.GET_OK, category, "查询成功");
        }else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }
}
